<?php
    require_once 'core/config.php';
    require_once 'core/string_helper.php';
    require_once 'core/url_helper.php';
    require_once 'core/loader.php';
    
    $model = load_model('crash_model');
    
    $crashes = $model->get_all();
    $upload_time = isset($_GET['time'])?$_GET['time']:NULL;
    $no_highlight = TRUE;
    if (count($crashes) > 0) {
        foreach ($crashes as &$crash) {
            $backtrace = $crash['backtrace'];
            $array_backtrace = explode("\n", $backtrace);
            $crash['crash_title'] = "";
            $crash['highlight'] = FALSE;
            if ($upload_time == $crash['upload_time']) {
                $crash['highlight'] = TRUE;
                $no_highlight = FALSE;
            }
            // get libsys.so first crash line
            if ($crash['process_type'] == PROCESS_TYPE_NATIVE) {
                $is_hydra = FALSE;
                foreach ($array_backtrace as $line) {
                    if (strstr($line, "libsys.so")) {
                        $crash['crash_title'] = $line;
                        $is_hydra = TRUE;
                        break;
                    }
                }
                if ($is_hydra == FALSE) {
                    $crash['crash_title'] = $crash['process'];
                }
            } else if ($crash['process_type'] == PROCESS_TYPE_JAVA) {
                foreach ($array_backtrace as $line) {
                    if (strstr($line, "Exception") || strstr($line, "Error")) {
                        $crash['crash_title'] = $line;
                        break;
                    }
                }
            }
        }
    }
    if ($upload_time != NULL && $no_highlight) {
        echo "No highlighted crash reports found, have you already exported that?";
    } else {
        load_view('main_view', array(
            'crash_reports'=>$crashes,
            'view_mode'=>VIEW_ALL
        ));
    }
?>
